//引入封装好的 service
import service from "./service";

// 声明一个基础接口变量
// let baseURL = 'http://192.168.122.124:8848/api/'; //124 nginx部署环境
// let baseURL = 'http://192.168.129.129:8848/api/'; //129测试环境
// let baseURL = 'http://192.168.129.129:8848/api/robot/'; //129测试环境
// let baseURL = ''; //124 后台部署环境
// let baseURL = 'http://localhost:8002/robot/'; //本地部署测试
let baseURL = "/api/"; //本地测试
// let baseURL = "/airobot/";

let token = "";
let userId = "";
let userName = "";
let schoolId = "";
let role = "";
let identityCode = "";
let version = null;
let userType = "";
let inputDevice = "1";

//设置请求头（如果请求头统一的话可以在axios文件设置，则无须从这里传过去）
let header = {
  Accept: "application/json;charset=UTF-8",
  Authorization: "",
  device: "COMPUTER",
  userId: "",
};

export const setConfig = (config) => {
  token = config.token;
  userId = config.userId;
  schoolId = config.schoolId;
  role = config.role.toUpperCase();
  identityCode = config.identityCode;
  version = config.version + "";
  userType = config.userType;
  userName = config.userName;

  if (process.env.NODE_ENV === "production") {
    baseURL = config.addr; // 打包配置
  }

  header = {
    Accept: "application/json;charset=UTF-8",
    Authorization: token,
    device: "COMPUTER",
    userId: userId,
  };
};

export const setIdentityCode = (code) => {
  identityCode = code;
};

/**
 * 获取高频指令
 * @param {*} data userId, schoolId
 * @returns
 */
export const getHighFrequency = () => {
  let data = {
    userId, //用户id
    schoolId, //学校id
    role, //角色
    userType,
    identityCode,
    version,
    inputDevice,
  };
  const url = baseURL + "queryHighFrequentInstruct";

  return service.post(url, data, header);
};

/**
 * 获取用户的再学习指令
 * @returns
 */
export const getRelearnInstructByUserId = () => {
  let data = {
    userId,
    schoolId,
    inputDevice,
  };
  const url = baseURL + "queryRelearnInstructByUserId";

  return service.get(url, data, header);
};

export const api_getHighServer = (trigger = "0", userType) => {
  let data = {
    trigger,
    schoolId,
    userType,
    userId,
    identityCode,
  };

  const url = baseURL + "HUOQUFUWU";

  return service.get(url, data, header);
};

/**
 *
 * @param {*} data userQuestion, userId, schoolId
 * @returns 返回【模糊列表、精确数据、未识别数据】
 */
export const executeRobot = (question, buriedId) => {
  let data = {
    userId, //用户id
    userQuestion: question, //用户问题
    schoolId, //学校id
    role, //角色
    identityCode,
    version,
    userType,
    inputDevice,
    userName,
    buriedId,
  };

  const url = baseURL + "executeRobotInstruct";

  return service.post(url, data, header);
};

/**
 * 获取数据返回数据
 * @param {*} data
 * @returns 【返回精确数据】
 */
export const getRobotAnswer = (instruct) => {
  const url = baseURL + "getRobotReactWithDataReturn";

  let data = {
    userId, //userid
    instructCode: instruct.instructCode, //指令编码
    instructName: instruct.instructName, //指令名称
    instructType: instruct.instructType, //指令类型
    instructSource: instruct.instructSource, //指令来源
    subSystemId: instruct.subSystemId || instruct.sysId, //子系统id
    funcName: instruct.funcName, //调用方法名
    method: instruct.method, // 地址后缀
    suffixUrl: instruct.suffixUrl, // 地址后缀
    schoolId, //学校id
    params: instruct.params,
    headers: instruct.headers,
    identityCode,
  };

  return service.post(url, data, header);
};

/**
 * 获取机器人打开操作地址
 * @param {*} data
 * @returns
 */
export const getRobotOpen = (instruct) => {
  const url = baseURL + "getRobotReactWithOpenOperation";

  let data = {
    userId, //userid
    instructCode: instruct.instructCode, //指令编码
    instructName: instruct.instructName, //指令名称
    instructType: instruct.instructType, //指令类型
    instructSource: instruct.instructSource, //指令来源
    subSystemId: instruct.subSystemId || instruct.sysId, //子系统id
    funcName: instruct.funcName, //调用方法名
    suffixUrl: instruct.suffixUrl, // 地址后缀
    schoolId, //学校id
    identityCode,
    moduleId: instruct.moduleId,
  };

  return service.post(url, data, header);
};

/**
 *
 * @param {*} data userQuestion, userId, schoolId
 * @returns 返回【模糊列表、精确数据、未识别数据】
 */
export const executeRobotRestrict = (instruct, question) => {
  let data = {
    schoolId, //学校id
    userId, //userid
    instructCode: instruct.instructCode, //指令编码
    instructName: instruct.instructName, //指令名称
    instructSource: instruct.instructSource, //指令来源
    instructType: instruct.instructType, //指令类型
    subSystemId: instruct.subSystemId || instruct.sysId, //子系统id
    restrictQuestion: question, // 问题
    params: instruct.params,
    headers: instruct.headers,
    funcName: instruct?.funcName, //调用方法名
    suffixUrl: instruct.suffixUrl, // 地址后缀
    method: instruct.method, // 地址后缀
    identityCode,
    moduleId: instruct.moduleId,
  };

  const url = baseURL + "getRobotReactWithRestrictWords";

  return service.post(url, data, header);
};

/**
 * 获取匹配的指令文本内容
 * @param {*} data
 * @returns
 */
export const getMatchList = (msg) => {
  let data = {
    userId, //用户id
    schoolId, //学校id
    userInput: msg, //输入文本
    role, //角色
    identityCode,
    version,
    inputDevice,
    userType,
  };

  const url = baseURL + "queryPossibleInstruct";

  return service.post(url, data, header);
};

/**
 * 获取所有聊天记录
 * @param {*} data
 * @returns
 */
export const getHistory = () => {
  let data = {
    userId,
    inputDevice,
  };

  const url = baseURL + "showHistory";

  return service.get(url, data, header);
};

/**
 * 获取所有聊天记录
 * @param {*} data
 * @returns
 */
export const saveHistory = (message, buriedId) => {
  let data = {
    userId,
    msgType: message.msgType,
    msgContent: message.msgContent,
    userType: message.userType, // 0：用户 1：机器人
    inputDevice,
    realMsgContent: message.realMsgContent || message.msgContent,
    buriedId,
  };

  const url = baseURL + "saveHistory";

  return service.post(url, data, header);
};

/**
 * 获取所有聊天记录
 * @param {*} data
 * @returns
 */
export const delHistory = (messageId, type) => {
  let data = {
    buriedId: messageId,
    type,
  };

  const url = baseURL + "delHistory";

  return service.post(url, data, header);
};

/**
 * 删除所有聊天记录
 * @param {*} data
 * @returns
 */
export const delAllHistory = () => {
  let data = {
    userId,
    inputDevice,
  };

  const url = baseURL + "delAllHistory";

  return service.post(url, data, header);
};

export const pointChat = (msgId) => {
  let data = {
    buriedId: msgId,
  };

  const url = baseURL + "pointChat";

  return service.post(url, data, header);
};

export const lahei = (msgId) => {
  let data = {
    buriedId: msgId,
  };

  const url = baseURL + "lahei";

  return service.post(url, data, header);
};

/**
 * 获取所有聊天记录
 * @param {*} data
 * @returns
 */
export const searchBase = (searchContent) => {
  let data = {
    userId, //用户id
    schoolId,
    searchContent: searchContent,
    role,
  };

  const url = baseURL + "search";

  return service.post(url, data, header);
};

export const queryConfigInfo = (instructCode) => {
  let data = {
    instructCode,
    schoolId,
    inputDevice,
    version,
  };

  const url = baseURL + "queryConfigInfo";

  return service.get(url, data, header);
};

export const api_downloadPdf = (source) => {
  let data = {
    identityCode,
    role,
    userId,
    schoolId,
    source,
    version,
    inputDevice,
  };

  const url = baseURL + "downloadPdf";

  return service.post(url, data, header);
};

export const api_getBaseUrl = async (token) => {
  const url = baseURL + "getBaseInfo";

  return service.get(url, null, { Authorization: token });
};

export const api_addUserReaction = async (
  userQuestion,
  invalidResponse,
  invalidType
) => {
  const url = baseURL + "addUserReaction";

  let data = {
    userId,
    schoolId,
    device: "COMPUTER",
    userQuestion,
    role,
    identityCode,
    version,
    invalidResponse,
    inputDevice,
    invalidType,
    userName,
  };

  return service.post(url, data, header);
};
